<?php

/**
 * 默认控制器
 */
class Controller_Operatereport extends Controller_Abstract
{
    /* ******************************************************************** */
    function actionOutput()
    {
        $show_box['title'] = '查询操作';
		$this->_view['form'] = new Form_OpSearch(url('default::operatereport/indexo'));
		//查询，导出和批量删除
        //dump($_SESSION);
    }

	/* ******************************************************************** */
    function actionExcel()
    {
        //$show_box['title'] = '操作报告导出EXCEL';
		//$this->_view['form'] = new Form_OpSearch(url('default::operatereport/index'));
		//导出
        dump($_POST);

		// create a simple 2-dimensional array
		$data = array(
				1 => array ('Name', 'Surname'),
				array('Schwarz', 'Oliver'),
				array('Test', 'Peter')
				);

		// generate file (constructor parameters are optional)
		$xls = new Helper_ExcelXML('UTF-8', false, 'My Test Sheet');
		$xls->addArray($data);
		$allt = $xls->generateXMLOut('my-test');

		$cont =fopen("output.xls",'w');
		fwrite($cont,$allt);
		fclose($cont);
    }

	/* ******************************************************************** */
    function actionDelete()
    {
        //$show_box['title'] = '操作报告导出EXCEL';
		//$this->_view['form'] = new Form_OpSearch(url('default::operatereport/index'));
		//批量删除
        //dump($_POST);

		if ($this->_context->isPOST())
			{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				//$this->_view['startd']=$_POST['startdate'];
				//$this->_view['endd']=$endd;
				$addconds="acttime >='".$_POST['startdate']."' and acttime<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);

				$table =QDB::getConn();
				$sql = "DELETE FROM `operatelog` WHERE ".$addconds.";";
				//echo $sql;
				$table->execute($sql);
		}
		
    }

    /* ******************************************************************** */
    function actionIndex()
    {
        //dump($_SESSION);
		$show_box['title'] = '操作报告首页';
        if ($this->_app->currentUserRoles())
        {
			$data = array(
				1 => array ('日期', '用户名', '用户类型', 'IP地址', '操作内容')
				);
			
			$addconds="";
			$this->_view['startd']='';
			$this->_view['endd']='';
			if ($this->_context->isPOST())
			{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="acttime >='".$_POST['startdate']."' and acttime<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
			}

			if($this->_context->startd !='' && $this->_context->endd !=''){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="acttime >='".$this->_context->startd."' and acttime<='".$this->_context->endd."'";
			}
			
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Operate::find($addconds)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['oplogs'] = $select->order("id desc")->getAll();
			$select1 = Operate::find($addconds);
			$this->_view['oplogs1'] = $select1->order("id desc")->getAll();
			if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;
			//echo $this->_view['pager']['record_count'];
			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'operatereport|index|','\')',5);
			$this->_view['pagestr']= $pagediv->show();

			//Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],'查看'.$show_box['title'],'');

			//导出EXCEL
			if(isset($_POST['excel'])){
			 foreach ($this->_view['oplogs1'] as $st){
				array_push($data,array($st->acttime, $st->username, $st->usertype, $st->ipaddr, $st->operate));
			 }
			//array_push($data,array('1', '2', '2', '2', '2'));
			// generate file (constructor parameters are optional)
			$xls = new Helper_ExcelXML('UTF-8', false, 'My Test Sheet');
			$xls->addArray($data);
			$allt = $xls->generateXMLOut('my-test');

			$cont =fopen("output.xls",'w');
			fwrite($cont,$allt);
			fclose($cont);

			}
			
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	function actionIndexo()
    {
		$show_box['title'] = '操作报告首页';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],'查看'.$show_box['title'],'');
		
		$addconds='';$stime='';$etime='';
		if ($this->_context->isPOST())
			{
				if(isset($_POST['startdate']) && isset($_POST['startdate'])){
				
					if($_POST['startdate']!='' && $_POST['startdate']!=''){

						if($_POST['startdate']==$_POST['enddate']){
							$endd=$this->my_date_add('d',$_POST['enddate'],1);
						}else{
							$endd=$_POST['enddate'];
						}
						$this->_view['startd']=$_POST['startdate'];
						$this->_view['endd']=$endd;
						$stime=$_POST['startdate'].' 00:00:00';
						$etime=$endd.' 23:59:59';
						$addconds="acttime >='".$_POST['startdate']." 00:00:00' and acttime<='".$endd." 23:59:59'";

					}else{
						$addconds='';
					}

				}else{
					$addconds='';
				}
			}

			if($this->_context->startd !='' && $this->_context->endd !=''){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="acttime >='".$this->_context->startd."' and acttime<='".$this->_context->endd."'";
			}

		//$addconds="acttime>='2010-03-21 00:00:00' and acttime <='2010-03-21 23:59:59'";
		$demo = array();
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		//$_REQUEST['start']
		$page = ($_REQUEST['start']/$_REQUEST['limit'])+1;
		$this->_view['pageno']=$page;
		//$select = AlarmTable::find()->limitPage($_REQUEST['start'], $_REQUEST['limit']);
		if($stime!='' && $etime!=''){
			$select = Operate::find("acttime>=? and acttime<=?",$stime,$etime)->limitPage($page,$_REQUEST['limit']);
		}else{
			$select = Operate::find()->limitPage($page,$_REQUEST['limit']);
		}
		$allc=$select->getPagination();
		$totaldata = $allc['record_count'];
		$demostr=$select->order("id desc")->getAll()->toArray();
		$date = date("Y-m-d");
		for($i=0; $i<count($demostr); $i++){
			$demo[] = array("date"=>$demostr[$i]['id'],"pageview"=>$demostr[$i]['username'],"ip"=>$demostr[$i]['ipaddr'],"visits"=>$demostr[$i]['usertype'],"uv"=>$demostr[$i]['operate'],"pvuv"=>$demostr[$i]['operate'],"op"=>$demostr[$i]['acttime']);
			$date = date("Y-m-d",strtotime($date)-24*3600);
			
		}
		

		//$demo_out = array_slice($demo,$_REQUEST['start'],$_REQUEST['limit']);
		$str = '({"total":"'.$totaldata.'","root":'.json_encode($demo).'})';
		//dump($demostr);
		echo $str;
    }

	function my_date_add($opt,$time,$num){
		$year = date('Y',strtotime($time));
		$month = date('m',strtotime($time));
		$day = date('d',strtotime($time));
		$last_day = date('t',strtotime($time));
		if($opt=='y'){
			$year = $year + $num;
		}else if($opt=='d'){
			$day = $day + $num;
		}else if($opt=='m'){
			$month = $month + $num;
			if($day == $last_day){//如果是最后一天的话
				$day = date('t',strtotime("{$year}-{$month}-01"));
			}
		} else {
			return $time;
		}
		return date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
	}
}